# !/usr/bin/python3
# -*- coding: utf-8 -*-

"""
日志信息表控制器类
----------------------------------------------------
@Project :   xinhou-openai-framework
@File    :   Logs.py
@Contact :   sp_hrz@qq.com

@Modify Time      @Author    @Version    @Desciption
------------      -------    --------    -----------
2023/04/16 22:04  peng.shen   v1.0.0     None
"""

from fastapi import APIRouter, Depends
from fastapi.params import Path
from loguru import logger
from sqlalchemy.orm import Session

from apps.admin.schema.LogsSchema import ResLogsDetailSchema, ReqLogsFindSchema, ReqLogsSaveSchema, \
    ReqLogsUpdateSchema
from common.entity.Logs import Logs
from common.service.LogsService import LogsService
from xinhou_openai_framework.core.db.DatabaseManager import DatabaseManager
from xinhou_openai_framework.core.exception.GlobalExceptionType import ParameterException
from xinhou_openai_framework.core.reponse.R import R
from xinhou_openai_framework.core.reponse.ResModel import ResModel
from xinhou_openai_framework.pages.PageHelper import PageResultHelper, PageHelper

api = APIRouter()


@api.post('/admin/logs/find',
          response_model=ResModel[PageResultHelper[ResLogsDetailSchema]],
          tags=["admin", "logs"],
          summary="查询日志信息表信息带分页接口",
          description="通过参数模型传递条件查询")
async def find(search: PageHelper[ReqLogsFindSchema], db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[body]:{}".format(search.model_dump()))
    return R.SUCCESS(LogsService(db).find_by(search))


@api.post('/admin/logs/findAll',
          tags=["admin", "logs"],
          response_model=ResModel[PageResultHelper[ResLogsDetailSchema]],
          summary="查询日志信息表信息带分页接口",
          description="通过参数模型传递条件查询")
async def find_all(search: ReqLogsFindSchema, db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[body]:{}".format(search.model_dump()))
    return R.SUCCESS(LogsService(db).find_all(search))


@api.post("/admin/logs/save",
          tags=["admin", "logs"],
          response_model=ResModel[ResLogsDetailSchema],
          summary="保存日志信息表信息接口",
          description="通过参数模型保存数据")
async def save(model: ReqLogsSaveSchema, db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[body]:{}".format(model.model_dump_json()))
    return R.SUCCESS(LogsService(db).save(Logs(**model.model_dump(exclude_unset=True))))


@api.get("/admin/logs/delete/{id}",
         tags=["admin", "logs"],
         response_model=ResModel[bool],
         summary="删除日志信息表信息接口",
         description="根据ID删除数据")
async def delete(id: int = Path(title="ID不能为空"), db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[params]:{}".format({"id": id}))
    if id is None:
        raise ParameterException()
    return R.SUCCESS(LogsService(db).delete(Logs(id=id)))


@api.post("/admin/logs/update",
          tags=["admin", "logs"],
          response_model=ResModel[ResLogsDetailSchema],
          summary="更新日志信息表信息接口",
          description="根据ID更新数据")
async def update(model: ReqLogsUpdateSchema, db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[body]:{}".format(model.json()))
    if model.id is None:
        raise ParameterException()
    return R.SUCCESS(LogsService(db).update(Logs(**model.dict(exclude_unset=True))))


@api.get("/admin/logs/detail/{id}",
         tags=["admin", "logs"],
         response_model=ResModel[ResLogsDetailSchema],
         summary="获取日志信息表详情接口",
         description="根据ID获取数据")
async def detail(id: int = Path(title="ID不能为空"), db: Session = Depends(DatabaseManager().get_session)):
    logger.info("[params]:{}".format({"id": id}))
    if id is None:
        raise ParameterException()
    return R.SUCCESS(LogsService(db).find_by_id(Logs(id=id)))
